<?php

/*
** Function to get the current website's WEB ADMIN URL
** returns the web admin url
*/
function getWebAdminURL() {
	// Iterate through the Framework module's context variable to pickup the current Site URL
	if (isset($GLOBALS[CONTEXT_KEY])) {
		foreach ($GLOBALS[CONTEXT_KEY]->SiteInfoList as $site) {
			if ($site->SiteId == $_COOKIE['CURRENT_CONTENT_SITE_ID']) {
				$site_url = $site->SiteUrl;
        $web_admin_url = web_admin_url($site_url);
			}
		}
	}

	if (isset($web_admin_url))
		return $web_admin_url;
	else
		return false;
}

/**
 * Get web admin url for Super Admin
 */
function getFrontEndAdminURL() {
  $sites = load_all_sites();
  if (is_array($sites)) {
    foreach ($sites as $siteid => $sitename) {
      if ($siteid == $_COOKIE['CURRENT_CONTENT_SITE_ID']) {
        $site_url = get_site_url($siteid);
        $web_admin_url = web_admin_url($site_url);
      }
    }
  }

  if (isset($web_admin_url))
		return $web_admin_url;
	else
		return false;
}

/**
 * Frame web admin url
 * @param <string> $web_admin_url
 * @return <string>
 *  webadmin url
 */
function web_admin_url($web_admin_url) {
  // Sanitize the URL by adding http if that is missing
  if (strcasecmp(substr($web_admin_url, 0, 4), "http") <> 0) {
    $web_admin_url = "http://" . $web_admin_url;
  }
  //Generate the admin URL
  if ($web_admin_url[strlen($web_admin_url)-1] <> '/') {
    $web_admin_url .= '/';
  }
  $web_admin_url .= "admin";

  return $web_admin_url;
}

?>